<?php
/**
 * Created by PhpStorm.
 * User: zcq
 * Date: 2018/10/26
 * Time: 10:31
 */

namespace app\admin\controller;


use app\admin\service\VoteDataService;
use app\admin\service\VoteService;
use app\admin\service\VoteStatisticsService;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

//投票数据统计
class VoteData extends Base
{
    public $dataService;
    public $voteService;
    public function __construct(VoteDataService $voteDataService,VoteService $voteService)
    {
        parent::__construct();
        $this->dataService = $voteDataService;
        $this->voteService = $voteService;
    }

    /**
     * 投票列表
     */
    public function index()
    {
        $id = input('vote_id/d');
        $keyword = input('keyword');
        $vote = $this->check($id);
        $data = $this->dataService->listPagination($id,$keyword);
        $this->assign('data',$data);
        $this->assign('vote',$vote);
        $this->assign('keyword',$keyword);

        return $this->fetch();
    }

    /**
     * 清空投票记录
     */
    public function emptyData()
    {
        $id = input('id');
        $vote = $this->check($id);
        $res = $this->dataService->initVote($id);
        return dataReturn($res['code'],$res['msg']);
    }

    /**
     * @return mixed
     * 统计
     */
    public function count()
    {
        $voteId = input('vote_id/d');
        $vote = $this->check($voteId);
        $order = input('order');
        $start = input('start');
        $end = input('end');
        $keyword = input('keyword');
        $itemId = input('item');
        //echarts 按每日投票统计数据
        $data = $this->dataService->count($vote,$start,$end,$order,$keyword,$itemId);
        $this->assign('data',$data);
        $this->assign('vote',$vote);
        $this->assign('start',$start?:'');
        $this->assign('end',$end?:'');
        $this->assign('order',$order?:'');
        $this->assign('keyword',$keyword);
        $this->assign('item',$itemId);
        $this->assign('items',$this->dataService->getItems($voteId));
        $this->assign('chartData',json_encode($data['chartData']));
        return $this->fetch();
    }

    public function downloadCount()
    {
        $voteId = input('vote_id/d');
        $vote = $this->check($voteId);
        $start = input('start');
        $end = input('end');
        $keyword = input('keyword');
        $itemId = input('item');

        $list = $this->dataService->countList($voteId,$itemId);

        $speadSheet = new Spreadsheet();
        $speadSheet->setActiveSheetIndex(0);
        $speadSheet->getActiveSheet()
            ->setCellValue('A1', '手机号码')
            ->setCellValue('B1', '投票数');

        if ($list) {
            foreach ($list as $k => $v) {
                $speadSheet->getActiveSheet()
                    ->setCellValueExplicit('A' . ($k + 2), $v['mobile'],DataType::TYPE_STRING)
                    ->setCellValue('B' . ($k + 2), $v['total']);
            }
        }
        $filename = $vote['name'] . '.xlsx';
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="' . $filename . '"');
        header('Cache-Control: max-age=0');
        $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($speadSheet, 'Xlsx');
        $writer->save('php://output');
    }


    /**
     * @return mixed
     * 某个手机的投票记录
     */
    public function infoByMobile()
    {
        $id = input('vote_id/d');
        $vote = $this->check($id);
        $mobile = input('mobile');
        $data = $this->dataService->countByMobile($id,$mobile);
        $this->assign('data',$data);
        $this->assign('vote',$vote);
        $this->assign('mobile',$mobile);
        return $this->fetch();
    }


    public function check($voteId)
    {
        $vote = $this->voteService->check($voteId);
        if(!$vote)
            $this->error('不存在');
        return $vote;
    }
}